package com.next

import grails.gorm.transactions.Transactional
import grails.validation.ValidationException
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
class RiskWarningConfigRuleController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        params.offset = params.offset ? params.offset.toInteger() : 0
        max = params.max
        def name = params['name']
        def offset = params.offset
        def sql = "  from RiskWarningConfigRule m where 1=1 "
        if (name)
        {
            sql += " and name like '%${name}%' "
        }
        def sql1 = " select count(*) " + sql
        sql += "order by m.priority asc "
        def riskWarningConfigRuleList = RiskWarningConfigRule.executeQuery(sql, [max: max, offset: offset]).asList()
        def count = RiskWarningConfigRule.executeQuery(sql1)
        respond riskWarningConfigRuleList, model: [riskWarningConfigRuleList: riskWarningConfigRuleList, riskWarningConfigRuleCount: count[0]]
    }

    def show(RiskWarningConfigRule riskWarningConfigRule)
    {
        respond riskWarningConfigRule
    }

    def create()
    {
        params['targetUri'] = request.getHeader("referer")
        respond new RiskWarningConfigRule(params)
    }

    @Transactional
    def save(RiskWarningConfigRule riskWarningConfigRule)
    {

        println riskWarningConfigRule?.errors

        if (riskWarningConfigRule == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (riskWarningConfigRule.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond riskWarningConfigRule.errors, view: 'create'
            return
        }

        riskWarningConfigRule.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'riskWarningConfigRule.label', default: 'RiskWarningConfigRule'), riskWarningConfigRule.id])
                redirect url: params['targetUri']
            }
            '*' { respond riskWarningConfigRule, [status: CREATED] }
        }
    }

    def edit(RiskWarningConfigRule riskWarningConfigRule)
    {
        def targetUri = request.getHeader("referer")
        params['targetUri'] = targetUri
        respond riskWarningConfigRule
    }

    @Transactional
    def update(RiskWarningConfigRule riskWarningConfigRule)
    {
        if (riskWarningConfigRule == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (riskWarningConfigRule.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond riskWarningConfigRule.errors, view: 'edit'
            return
        }

        riskWarningConfigRule.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'riskWarningConfigRule.label', default: 'RiskWarningConfigRule'), riskWarningConfigRule.id])
                redirect url: params['targetUri']
            }
            '*' { respond riskWarningConfigRule, [status: OK] }
        }
    }

    @Transactional
    def delete(RiskWarningConfigRule riskWarningConfigRule)
    {

        if (riskWarningConfigRule == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        riskWarningConfigRule.delete flush: true

        def targetUri = request.getHeader("referer")
        def riskWarningConfig = riskWarningConfigRule.riskWarningConfig
        if (!RiskWarningConfigRule.findByRiskWarningConfig(riskWarningConfig))
        {
            riskWarningConfig.status = 0
            riskWarningConfig.save flush: true
        }

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'riskWarningConfigRule.label', default: 'RiskWarningConfigRule'), riskWarningConfigRule.id])
                redirect url: targetUri
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'riskWarningConfigRule.label', default: 'RiskWarningConfigRule'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }
}
